<template>
    <el-form :model="shangjia" label-position="right" label-width="auto">
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="商家名" prop="name">
                    <el-input v-model="shangjia.name" placeholder="请输入商家名" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="地址" prop="address">
                    <el-input v-model="shangjia.address" placeholder="请输入地址" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="起送费" prop="qsf">
                    <el-input v-model="shangjia.qsf" placeholder="请输入起送费" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="配送费" prop="psf">
                    <el-input v-model="shangjia.psf" placeholder="请输入配送费" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="打包费" prop="dbf">
                    <el-input v-model="shangjia.dbf" placeholder="请输入打包费" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="状态：" prop="status">
                    <el-select v-model="shangjia.status" placeholder="状态" style="width: 240px">
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>

    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits([
    "closeDialog", // 关闭 dialog
    "flushPage"    // 刷新页面
]);

const shangjia = ref({
    name: '',
    address: '',
    qsf: '',
    psf: '',
    dbf: '',
    status: ''
});

const options = [
    {
        value: '营业中'
    },
    {
        value: '关门中'
    },
    {
        value: '暂停营业'
    }
];

const handleUpdateData = (obj) => {
    shangjia.value = obj;
};

// 暴露给父组件使用的方法
defineExpose({
    handleUpdateData,
});

// 取消
const doCancel = () => {
    emits("closeDialog");
};

// 更新
const doSave = async () => {
    const { data } = await axios.put('/api/shangjia', shangjia.value);
    if (data.code === 200) {
        emits("flushPage"); // 触发事件： 刷新页面
        emits("closeDialog"); // 触发事件：关闭 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
};
</script>

<style scoped>

</style>